# coding:utf-8

from typing import Dict
from service import stock_view_service
from views.template_page_view import BasePageTemplateView
from .common.translator import Translator
from common.utils.JsonReplace import JsonPlaceholderReplacer
from PyQt6.QtWidgets import (
    QHBoxLayout,
)


class StockRecommend(BasePageTemplateView):
    """Scroll interface"""

    def __init__(self, parent=None):
        t = Translator()
        super().__init__("StockRecommend", parent=parent)

    def doInitAndUpdate(self):

        viewService = stock_view_service()

        # 第一部分 ： 板块推荐面板
        level1_HLayout = QHBoxLayout()
        self.addEchatTemplate(
            "风险板块",
            "stock_recommend",
            self.exchangeRecommendData,
            self.getBaseParam,
            1,
            level1_HLayout,
        )
        self.addTableTemplate(
            "风险板块报表",
            self.tableMainStockRisk,
            self.getBaseParam,
            1,
            level1_HLayout,
        )
        self.addOutLayout(level1_HLayout)

        # 第二部分 : 推荐的板块
        level2_HLayout = QHBoxLayout()
        self.addEchatTemplate(
            "推荐板块",
            "stock_recommend",
            self.exchangeRecommendData,
            self.getBaseParam,
            1,
            level2_HLayout,
        )
        self.addTableTemplate(
            "推荐板块报表",
            self.tableMainStockRisk,
            self.getBaseParam,
            1,
            level2_HLayout,
        )
        self.addOutLayout(level2_HLayout)

        # 第三部分 ：推荐关注的股票
        level3_HLayout = QHBoxLayout()
        self.addEchatTemplate(
            "推荐股票",
            "stock_recommend",
            self.exchangeRecommendData,
            self.getBaseParam,
            1,
            level3_HLayout,
        )
        self.addTableTemplate(
            "推荐股票报表",
            self.tableMainStockRisk,
            self.getBaseParam,
            1,
            level3_HLayout,
        )
        self.addOutLayout(level3_HLayout)

        # 第四部分 ：推荐关注的股票
        level4_HLayout = QHBoxLayout()
        self.addEchatTemplate(
            "推荐股票",
            "stock_recommend",
            self.exchangeRecommendData,
            self.getBaseParam,
            1,
            level4_HLayout,
        )
        self.addTableTemplate(
            "推荐股票报表",
            self.tableMainStockRisk,
            self.getBaseParam,
            1,
            level4_HLayout,
        )
        self.addOutLayout(level4_HLayout)

    def getBaseParam(self) -> Dict[str, str]:
        return {"stockCode": "100001"}

    def exchangeRecommendData(self, jsonData: str, params: Dict[str, str]) -> str:
        source_data = [
            {
                "value": 99,
                "name": "半导体",
                "path": "10001",
                "children": [
                    {"value": 33, "name": "半导体1", "path": "Accounts/Access"},
                    {"value": 2, "name": "半导体2", "path": "Accounts/Authentication"},
                    {"value": 80, "name": "半导体3", "path": "Accounts/Notification"},
                ],
            },
            {
                "value": 15,
                "name": "金融证券",
                "path": "Accounts",
                "children": [
                    {"value": 76, "name": "国盛金控", "path": "Accounts/Access"},
                    {
                        "value": 92,
                        "name": "东方财富",
                        "path": "Accounts/Authentication",
                    },
                ],
            },
        ]
        replacer = JsonPlaceholderReplacer(jsonData)
        replacer.replace_placeholder("__STOCK_DATA__", source_data)
        replacer.replace_placeholder("__STOCK_NAME__", "板块推荐")

        return replacer.get_modified_json()

    def tableMainStockRisk(self, params: Dict[str, str]) -> str:
        try:
            # 定义表格头
            table_header = [
                "策略名称",
                "风险等级",
                "策略分组",
                "分析天数",
                "节点得分",
                "描述",
            ]

            # 填充表格数据
            table_data = []
            for assessment in self.finalResult.risk_assessments:
                row = [
                    assessment.config.strategy_name,
                    str(assessment.config.strategy_level),
                    (
                        "风险型"
                        if assessment.config.strategy_group == StrategyGroup.RISK
                        else "关注型"
                    ),
                    str(assessment.config.analysis_day),
                    str(assessment.node_point),
                    assessment.description,
                ]
                table_data.append(row)

                # 定义操作按钮回调函数

            def example_callback(stock_code: str) -> str:
                return f"查看股票 {stock_code} 的详情"

            # 创建 TableDataDto 对象
            table_item_fun = [{"详情": example_callback}]
            return TableDataDto(
                table_header=table_header,
                table_data=table_data,
                table_item_fun=table_item_fun,
            )

        except Exception as e:
            AntLogger.info(f"转换时出错: {e}")
            # 返回一个空的 TableDataDto 对象
            return TableDataDto(table_header=[], table_data=[], table_item_fun=[])
